using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Configuration;

namespace AdaptiveHelpDesk
{
    class GlobalMethods
    {
         //Method to updateUserModel COMBO BOX
        public void updateUSERMODELcmb(UpdateUserModelComboBox UM,decimal tTime,int yMouseV, int yMouseA, int Dtime, 
                                                               int avgDwellTime, int nrItems, int nrUniqueItems, int sTime, double KLMRatio, double KLMDifference)
        {
            //LOW-LEVEL MOTION CHARACTERISTICS
            UM.updateTotalTime(tTime);
            UM.updateYMouseVelocity(yMouseV);
            UM.updateYMouseAcceleration(yMouseA);
            UM.updateDwellTime(Dtime);
            
            //INTERACTION TECHNIQUE
            UM.updateAverageDwellTime(avgDwellTime);
            UM.updateNrItemsVisited(nrItems);
            UM.updateUniqueItemsVisited(nrUniqueItems);
            UM.updateSelectionTime(sTime);

            //PERFORMANCE MODEL
            UM.updateKLMDifference(KLMDifference);
            UM.updateKLMRatio(KLMRatio);

        }



        #region ADAPTIVE ALGORITHM


            private ArrayList addToTop3(string userID, string listName, StoredProcedures sp)
            {
                string recentlyUsedItem = sp.getRecentlyUsedItem(userID, listName);
                ArrayList arrayFrequentlyUsedItems = (ArrayList)sp.getTop3FrequentlyUsedItems(userID, listName);

                // 1) add the most recently used item and the 2 most frequently used items to the TOP 3 list 

                // 2.1) if recently used item is a frequently used items OR
                // 2.2) if there is no recently used item for that list
                if (arrayFrequentlyUsedItems.Count >= 3)
                {
                    if ((arrayFrequentlyUsedItems[0].ToString().Equals(recentlyUsedItem)) || (arrayFrequentlyUsedItems[1].ToString().Equals(recentlyUsedItem)) || (recentlyUsedItem.Equals("")))
                    {

                    }
                    else
                    {

                        arrayFrequentlyUsedItems[2] = recentlyUsedItem; ;
                    }
                }
                return arrayFrequentlyUsedItems;
            }


            public BindingList<string> BaseAdaptiveAlgorithm(string userID, string listName, StoredProcedures sp, BindingList<string> sortedListItems)
            {
                ArrayList arrayTop3 = (ArrayList)addToTop3(userID,listName,sp);

                int count = 0;

                for (int i = 0; i <= sortedListItems.Count - 1; i++)
                {
                    string item = sortedListItems[i].ToString();

                    if (sortedListItems[i].Equals(arrayTop3[0]))
                    {
                        sortedListItems.RemoveAt(i);
                        sortedListItems.Insert(count, item);
                        count++;
                    }
                    else if (sortedListItems[i].Equals(arrayTop3[1]))
                    {
                        sortedListItems.RemoveAt(i);
                        sortedListItems.Insert(count, item);
                        count++;
                    }
                    else if (sortedListItems[i].Equals(arrayTop3[2]))
                    {
                        sortedListItems.RemoveAt(i);
                        sortedListItems.Insert(count, item);
                        count++;
                    }
                }

                return sortedListItems;

            }


            public void UpdateOrInsertRecentlyUsedItem(string userID, string listName, string itemName, StoredProcedures sp)
            {
                string recentlyUsedItem = sp.getRecentlyUsedItem(userID, listName);

                //If item doesn't exist then ADD item
                if (recentlyUsedItem.Equals(""))
                {
                    sp.insertRecentlyUsedItem(userID, listName, itemName);
                }
                //otherwise UPDATE item
                else
                {
                    sp.updateRecentlyUsedItem(userID, listName, itemName);
                }
            }


            public void UpdateFrequentlyUsedItem(string userID, string listName, string itemName, StoredProcedures sp)
            {
                int frequency = sp.getFrequency(userID, listName, itemName);
                frequency++;
                sp.updateFrequentlyUsedItem(userID, listName, itemName, frequency);
            }
          

        #endregion
    }
}
